home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / ms.zip / ARC5_4.CHP < prev    next >
Text File  |  1993-06-15  |  3KB  |  97 lines

  1. %
  2. #EF
  3. #T15,1,Chapter 5     General Design Considerations     Pg. 14
  4. #HS,1,4,80,25,11,1
  5. #C4,R5
  6.                       ~W~IThe Mysterious Magic Number~Y~I
  7.  
  8. In addition to the other things we've discussed in this chapter, it's also
  9. a good idea to avoid what are called ~W~Imagic numbers~Y~I. A magic number is a
  10. number in program code that is not self-explanatory. Most numbers aren't
  11. self-explanatory. Here's an example of a loop containing a magic number.
  12.  
  13.  
  14.                                  ~W~IFigure 5.4~Y~I
  15.                           A loop with a magic number
  16.  
  17.  
  18.     for (i=0;i<~W~I35~Y~I;i++)    /* Why does the loop go to 35? You */
  19.     {                     /* can't tell just by looking at it. */
  20.         /* Code goes in here. */
  21.                  .
  22.                  .
  23.                  .
  24.     }
  25.  
  26. #WN
  27. #BO,4,8,78,12,7,1,0,2,15,6
  28. Can you tell me what that loop does? Probably not. Now let's look at the
  29. same loop with a named constant used to replace the magic number.
  30.  
  31. #WP
  32. %
  33. #EF
  34. #T15,1,Chapter 5     General Design Considerations     Pg. 15
  35. #HS,1,4,80,25,11,1
  36. #C4,R5
  37.                             ~W~IFigure 5.5~Y~I
  38.                    The same loop without the magic number
  39.  
  40.     for (i=0;i<|MAX_STUDENTS_PER_CLASS|;i++)
  41.     {
  42.         /* Code goes in here. */
  43.                   .
  44.                   .
  45.                   .
  46.     }
  47. #WN
  48. #C4,R17
  49. Now do you know what it does? In the first example, the number 35 could have
  50. meant ~W~Ianything~Y~I. But anyone with a little experience in C can look at the
  51. second example and see that it does some sort of processing on a classroom
  52. of students. The loop isn't seen as running from 0 to 34. We now see it as
  53. running from an empty class to a full class. The latter is much clearer and
  54. ~G~Imore closely models the real world~Y~I.
  55.  
  56. #WP
  57. %
  58. #EF
  59. #T15,1,Chapter 5     General Design Considerations     Pg. 16
  60. #HS,1,4,80,25,11,1
  61. #C4,R5
  62. ~Y~IEven if we only use a magic number once in a given program, it is wise
  63. to define a named constant for it. Using named constants helps make the
  64. code self-documenting.
  65.  
  66. #WN
  67. Staying away from magic numbers also helps the program to be more
  68. modifiable. Imagine that I wrote a program using the loop in Figure 5.4.
  69. Now let's ask ourselves what would happen if I were to have several dozen
  70. more loops in the same program, all of which ran from 0 to 34. The school
  71. board meets and decides that the maximum number of students per class is
  72. now 37.
  73.  
  74. #WN
  75. ~KThe software must now be changed. If I have used the literal number
  76. 35 in several dozen loops, I must go back and change ~R~Iall of those loops~Y~I. If
  77. I have used a named constant as the upper limit of all those loops, I only
  78. have to go back and ~G~Ichange the number associated with the named constant~Y~I.
  79. When I re-compile, it will be changed everywhere the named constant appears.
  80. In other words, ~C~II've found another way to be lazy.~Y~I~k
  81.  
  82. #WP
  83. %
  84. #EF
  85. #T15,1,Chapter 5     General Design Considerations     Pg. 17
  86. #HS,1,4,80,25,11,1
  87. #C4,R5
  88. ~Y~I
  89. With the combination of building proper programs from proper program
  90. segments, information hiding, implementation hiding, and avoiding magic
  91. numbers, we can put together robust programs that are composed of highly
  92. reusable pieces. If we can reuse the pieces, we are being successfully
  93. lazy.
  94. #WP
  95. #X
  96.  
  97.